/**
 * 
 */
package net.yangying.androidframework.util;

import java.util.Iterator;
import java.util.Map;

import net.yangying.androidframework.R;
import net.yangying.androidframework.core.model.Group;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * @author yangying
 * @date 2013-5-29 上午9:49:05
 * @version 1.0
 *
 */
public class ViewUtil {
	
	public static final int COLUMN_SUM = 12;
	/**
	 * 首页上-左下
	 */
	public static final int LAYOUT_1 = 1;
	/**
	 * 首页左右
	 */
	public static final int LAYOUT_2 = 2;
	/**
	 * 仅文字
	 */
	public static final int LAYOUT_3 = 3;
	
	public static final int ID_RANDOM_HOME = 1000;
	public static final int ID_RANDOM_CATEGORY = 10000;
	
	public static final int ALAPHA_70 = 100;
	
	public static final int TITLE_MAX_NUM = 8;
	
	private static final int DOMAIN_TOGGLE_COLUMN_NUM = 4;

	
	
//	public static void initTableRowSite(Context context, Group<WebSiteGroup> group, RelativeLayout rl){
//		if(group!=null && group.size() > 0){
//			LinearLayout row = null;
//			int curRowNum = 0;
//			int lastRowNum = 0;
//			int lastRowId = 0;
//			for(int site = 0; site < group.size(); site++) {
//				WebSiteGroup webSiteGroup = group.get(site);
//				curRowNum = webSiteGroup.getLineNumber();
//				if(curRowNum != lastRowNum){
//					row = new LinearLayout(context);
//					java.util.Random r =new java.util.Random();
//					int i = r.nextInt(1000);
//					row.setId(i + site);
//					row.setOrientation(LinearLayout.HORIZONTAL);
//					LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//					row.setLayoutParams(lparams);
//					RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(row.getLayoutParams());
//					layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);  
//					if(site == 0){
//						layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//					} else {
//						layoutParams.addRule(RelativeLayout.BELOW, lastRowId);
//					}
//					rl.addView(row,layoutParams);
//					lastRowId = row.getId();
//					lastRowNum = curRowNum;
//				}
//				row.setGravity(Gravity.CENTER);				
//				View view = null;
//				if(webSiteGroup.getLayout() == 1){
//					view = (View)LayoutInflater.from(context).inflate(R.layout.home_category_1, null);	
//				} else if(webSiteGroup.getLayout() == 2){
//					view = (View)LayoutInflater.from(context).inflate(R.layout.home_category_2, null);
//				} else {
//					view = (View)LayoutInflater.from(context).inflate(R.layout.home_category_3, null);
//				} 
//				LinearLayout ll_home_category= (LinearLayout)view.findViewById(R.id.ll_home_category);
//				LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//				llp.weight = webSiteGroup.getColspan();
//				llp.setMargins(
//						DeviceUtil.dip2px(context, 2),
//						DeviceUtil.dip2px(context, 2), 
//						DeviceUtil.dip2px(context, 2), 
//						DeviceUtil.dip2px(context, 2));
//				ll_home_category.setLayoutParams(llp);
//				if(webSiteGroup.getLayout() == 1
//						||webSiteGroup.getLayout() == 2){
//					ImageView tv_home_category_icon= (ImageView)view.findViewById(R.id.iv_home_category_icon);
////					AsyncImage.setAsyncImage(webSiteGroup.getIconUrl(),map ,mRrm,mLaunchedPhotoFetches,tv_home_category_icon,bmp_default,false);
//				}
//				TextView tv_home_category_text= (TextView)view.findViewById(R.id.tv_home_category_text);
//				tv_home_category_text.setTextColor(context.getResources().getColor(R.color.white));
//				tv_home_category_text.setTextSize(16);
//				Drawable drawable = BackgroundParser.getDrawable(context, webSiteGroup.getBgColor());
//				view.setBackgroundDrawable(drawable);
//				
//				int alapha = webSiteGroup.getAlapha();
//				if(alapha >= 0 && alapha <=100){
//					view.getBackground().setAlpha(255 * ( alapha / 100 ));	
//				}
//				
//				tv_home_category_text.setText(webSiteGroup.getName());
//				ll_home_category.setTag(webSiteGroup);
////				ll_home_category.setOnClickListener(new OnClickListener() {
////					@Override
////					public void onClick(View v) {
//////						WebSiteGroup group = (WebSiteGroup)v.getTag();
//////						Intent intent = new Intent(HomeActivity.this,SiteCatalogActivity.class);
//////						Animation.set(R.anim.push_left_in, R.anim.push_left_out);
//////						intent.putExtra(Intents.APPGROUP, group);
//////						startActivity(intent);
//////						if(site.getUrl() != null){
//////							String url = site.getUrl().replaceAll(Constants.SPACE, Constants.EMPTY);
//////							if(url.length()  > 0){
//////								Intent intent = new Intent(HomeActivity.this,WapcnWebViewActivity.class);
//////								intent.putExtra(Intents.URL, url);
//////								Animation.set(R.anim.push_left_in, R.anim.push_left_out);
//////								startActivity(intent);
//////							}
//////						}
////					}
////				});
//				row.addView(view);
//			}
//		}
//	}
//	
//	public static void initTableLayout(final Context context, String[] g, final Map<String,ToggleButton> tBtnMap, TableLayout tl){
//		if(g.length > 0){
//			TableRow tr = null;
//			for(int i = 0; i < g.length; i++) {
//				if(tBtnMap.containsKey(g[i])){
//					continue;
//				}
//				if( i % DOMAIN_TOGGLE_COLUMN_NUM == 0){
//					tr = new TableRow(context);
//				}
//				tr.setGravity(Gravity.CENTER_VERTICAL);
//				View view = (View)LayoutInflater.from(context).inflate(R.layout.checkbox_item, null);
//				ToggleButton tbtn = (ToggleButton)view.findViewById(R.id.toggleBtn);
//				tbtn.setTag(g[i]);
//				tbtn.setText(g[i]);
////				tbtn.setLayoutParams(new ViewGroup.LayoutParams(0,40));
//				tbtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {					
//					@Override
//					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//						//关闭输入法窗口
////						InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);  
////				        imm.hideSoftInputFromWindow(aTv.getWindowToken(), 0);
//						String suffix = (String)buttonView.getTag();
//						//for the button of seletall 
//						if(Constants.SELECT_ALL.equals(suffix)){
//							Iterator it = tBtnMap.entrySet().iterator();
//							while(it.hasNext()){
//								Map.Entry entry = (Map.Entry) it.next(); 
//							    String key = (String)entry.getKey(); 
//							    ToggleButton tbtn = (ToggleButton)entry.getValue();
//							    if(isChecked){
//							    	tbtn.setChecked(true);	
//							    } else {
//							    	tbtn.setChecked(false);
//							    }
//							}
//						}
//					}
//				});
//				//default com,cn,net,org checked
//				if(Constants.SUFFIX_COM_DEFAULT.equals(g[i])
//						||Constants.SUFFIX_CN_DEFAULT.equals(g[i])
//						||Constants.SUFFIX_NET_DEFAULT.equals(g[i])
//						||Constants.SUFFIX_ORG_DEFAULT.equals(g[i])){
//					tbtn.setChecked(true);
//				} else {
//					tbtn.setChecked(false);
//				}
//				tBtnMap.put(g[i], tbtn);	
//				tr.addView(view);//文字按钮内部的间距
//				if(i % DOMAIN_TOGGLE_COLUMN_NUM == DOMAIN_TOGGLE_COLUMN_NUM -1  
//						|| i == g.length - 1){
//					tl.addView(tr);		
//				}
//				if(Constants.SUFFIX_ZHONGGUO.equals(g[i])
//	            		||Constants.SUFFIX_WANGLUO.equals(g[i])
//	            		||Constants.SUFFIX_GONGSI.equals(g[i])){
//					tbtn.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.checkbox_green));
//	            } else if(Constants.SELECT_ALL.equals(g[i])){
//	            	tbtn.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.checkbox_purple));
//	            } else {
//	            	tbtn.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.checkbox_blue));
//	            }
//			}
//		}
//	}
}
